package com.qqhf.hitfood.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qqhf.common.entity.AjaxResult;
import com.qqhf.common.entity.PageEntity;
import com.qqhf.hitfood.bean.core.BaseController;
import com.qqhf.hitfood.bean.dto.BizFoodDto;
import com.qqhf.hitfood.bean.dto.MenuListDto;
import com.qqhf.hitfood.bean.entity.BizFood;
import com.qqhf.hitfood.bean.entity.TypeFood;
import com.qqhf.hitfood.service.IBizFoodService;
import com.qqhf.hitfood.service.IBizShopService;
import com.qqhf.hitfood.service.ITypeFoodService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Api(tags = "B - 食品管理")
@RestController
@RequestMapping("/biz/food")
public class BizFoodController extends BaseController {
    @Autowired
    private IBizFoodService bizFoodService;
    @Autowired
    private ITypeFoodService typeFoodService;
    @Autowired
    private IBizShopService bizShopService;

    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity, BizFood bizFood) {
        return AjaxResult.success(bizFoodService.page(pageEntity.toPage(), Wrappers.lambdaQuery(bizFood)));
    }

    @GetMapping("/list")
    public AjaxResult list(BizFood bizFood) {
        return AjaxResult.success(toDto(bizFoodService.list(Wrappers.lambdaQuery(bizFood))));
    }

    @Operation(summary = "搜索食品：名称、类型、所属店铺")
    @GetMapping("/search")
    public AjaxResult search(
            @Parameter(name = "shopId", description = "所属店铺ID")
            @RequestParam(value = "shopId", required = false) Integer shopId,
            @Parameter(name = "keyword", description = "关键字")
            @RequestParam(value = "keyword", required = false) String keyword,
            @Parameter(name = "typeId", description = "食品类型ID")
            @RequestParam(value = "typeId", required = false) Integer typeId) {
        List<BizFood> result = bizFoodService.list();
        if (keyword != null) {
            List<BizFood> list = bizFoodService.list(new QueryWrapper<BizFood>().like("name", keyword));
            List<BizFood> introList = bizFoodService.list(new QueryWrapper<BizFood>().like("intro", keyword));
            result = Stream.of(list, introList).flatMap(Collection::stream).distinct().collect(Collectors.toList());
        }
        if (shopId != null) {
            result = result.stream().filter(item -> item.getShopId().equals(shopId)).collect(Collectors.toList());
        }
        if (typeId != null) {
            result = result.stream().filter(item -> item.getTypeId().equals(typeId)).collect(Collectors.toList());
        }
        List<BizFoodDto> bizFoodDtos = toDto(result);
        return AjaxResult.success(bizFoodDtos);
    }

    @Operation(summary = "获得菜单：店铺id")
    @GetMapping("/menu")
    public AjaxResult menu(
            @Parameter(description = "所属店铺ID")
            @RequestParam(value = "shopId") Integer shopId) {
        List<BizFood> foods = bizFoodService.list(new QueryWrapper<BizFood>().eq("shop_id", shopId));
        List<TypeFood> types = typeFoodService.list();
        List<MenuListDto> menu = new ArrayList<>();
        for (TypeFood typeFood : types) {
            int typeId = typeFood.getId();
            String typeName = typeFood.getName();
            List<BizFood> collect = foods.stream().filter(item -> item.getTypeId().equals(typeId)).collect(Collectors.toList());
            if (collect.size() > 0)
                menu.add(new MenuListDto(typeName, typeFood.getIntro(), collect));
        }
        return AjaxResult.success(menu);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody BizFood bizFood) {
        return AjaxResult.success(bizFoodService.save(bizFood));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody BizFood bizFood) {
        return AjaxResult.success(bizFoodService.updateById(bizFood));
    }

    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(bizFoodService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(bizFoodService.getById(id));
    }

    /**
     * 包装后类型从id变为文字名称
     *
     * @param list
     * @return
     */
    public List<BizFoodDto> toDto(List<BizFood> list) {
        List<BizFoodDto> bizFoodDtos = new LinkedList<>();
        for (int i = 0; i < list.size(); i++) {
            BizFood bizFood = list.get(i);
            BizFoodDto bizFoodDto = new BizFoodDto();
            BeanUtils.copyProperties(bizFood, bizFoodDto);
            bizFoodDto.setShopName(bizShopService.getById(bizFood.getShopId()).getName());
            bizFoodDto.setType(typeFoodService.getById(bizFood.getTypeId()).getName());
            bizFoodDtos.add(bizFoodDto);
        }
        return bizFoodDtos;
    }
}
